MEGpipeline_RemoveICAcomps.m 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2. % Reject selected ICA components and reconstruct MEG data. %
  3. % Last modified: July 25, 2014 %
  4. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  5. % Copyright (C) 2013-2014, Michael J. Cheung
  6. %
  7. % This file is a part of the MEG & PLS Pipeline (MEGPLS). For more
  8. % details, see the documentation included with the software package.
  9. %
  10. % MEGPLS is free software: you can redistribute it and/or modify it under
  11. % the terms of the GNU General Public License version 2 as published by
  12. % the Free Software Foundation. This program is distributed in the hope
  13. % that it will be useful, but WITHOUT ANY WARRANTY; without even the
  14. % implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. % See the GNU General Public License for more details.
  16. %
  17. % You should have received a copy of the GNU General Public License along
  18. % with this program. If not, you can download the license here:
  19. % <http://www.gnu.org/licenses/old-licenses/gpl-2.0>.
  20. function MEGpipeline_RemoveICAcomps(InputParams)
  21. % Make sure java paths added:
  22. UseProgBar = CheckParforJavaPaths;
  23. % Load InputParams:
  24. name = InputParams.name;
  25. paths = InputParams.paths;
  26. RejectComps = InputParams.RejectComps;
  27. % Set up errorlog and diary:
  28. if exist('ErrorLog_ICA.txt', 'file')
  29. system('rm ErrorLog_ICA.txt');
  30. end
  31. if exist('Diary_ICA.txt', 'file')
  32. system('rm Diary_ICA.txt');
  33. end
  34. diary Diary_ICA.txt
  35. ErrLog = fopen('ErrorLog_ICA.txt', 'a');
  36. %==============================%
  37. % CREATE ICA CLEANED DATASETS: %
  38. %==============================%
  39. for g = 1:length(name.GroupID)
  40. NumSubj = length(name.SubjID{g});
  41. if UseProgBar == 1
  42. ppm = ParforProgMon...
  43. (['REMOVING COMPONENTS & RECONSTRUCTING DATA: ',name.GroupID{g},'. '], NumSubj, 1, 700, 80);
  44. end
  45. parfor s = 1:length(name.SubjID{g})
  46. for c = 1:length(name.CondID)
  47. UnmixingInfo = LoadFTmat(paths.ICAunmixing{g}{s,c}, 'ICA');
  48. if isempty(UnmixingInfo)
  49. continue;
  50. end
  51. %--- If no components selected for rejection: ---%
  52. %------------------------------------------------%
  53. % Keep data as is:
  54. if isempty(RejectComps{g}{s,c})
  55. ICAcleanMEG = LoadFTmat(paths.InputPreprocMEG{g}{s,c}, 'ICA');
  56. if isempty(ICAcleanMEG)
  57. continue;
  58. end
  59. ICAcleanMEG.ICAcompRemoved = [];
  60. end
  61. %--- If components selected for rejection: ---%
  62. %---------------------------------------------%
  63. if ~isempty(RejectComps{g}{s,c})
  64. % Check input dataset:
  65. CheckInput = CheckPipelineMat(paths.InputPreprocMEG{g}{s,c}, 'ICA');
  66. if CheckInput == 0
  67. continue;
  68. end
  69. % Decompose original data using unmixing info:
  70. cfgICA = [];
  71. cfgICA = UnmixingInfo.cfgICA; % Use same settings
  72. cfgICA.unmixing = UnmixingInfo.unmixing;
  73. cfgICA.topolabel = UnmixingInfo.topolabel;
  74. cfgICA.inputfile = paths.InputPreprocMEG{g}{s,c};
  75. OrigComp = ft_componentanalysis(cfgICA);
  76. % Make sure OrigData has same channels as specified in cfgICA:
  77. % Due to ft_rejectcomponent keeping all channels in.
  78. cfgSelectChan = [];
  79. cfgSelectChan.channel = cfgICA.channel;
  80. cfgSelectChan.inputfile = paths.InputPreprocMEG{g}{s,c};
  81. OrigData = ft_selectdata_new(cfgSelectChan);
  82. % Reject components and reconstruct data:
  83. cfgReject = [];
  84. cfgReject.component = RejectComps{g}{s,c};
  85. cfgReject.demean = cfgICA.demean;
  86. ICAcleanMEG = ft_rejectcomponent(cfgReject, OrigComp, OrigData);
  87. ICAcleanMEG.ICAcompRemoved = RejectComps{g}{s,c};
  88. % Make sure AllEventInfo field is carried over:
  89. if ~isfield(ICAcleanMEG, 'AllEventInfo') && isfield(OrigData, 'AllEventInfo')
  90. ICAcleanMEG.AllEventInfo = OrigData.AllEventInfo;
  91. end
  92. OrigComp = []; % Free memory immediately
  93. OrigData = [];
  94. end
  95. % Save ICA cleaned data:
  96. CheckSavePath(paths.ICAcleanData{g}{s,c}, 'ICA');
  97. ParforSaveData(paths.ICAcleanData{g}{s,c}, ICAcleanMEG);
  98. ICAcleanMEG = []; % Free memory
  99. end % Cond
  100. if UseProgBar == 1 && mod(s, 1) == 0
  101. ppm.increment(); % move up progress bar
  102. end
  103. end % Subj
  104. if UseProgBar == 1
  105. ppm.delete();
  106. end
  107. end % Group
  108. %=========================%
  109. % CHECK FOR OUTPUT FILES: %
  110. %=========================%
  111. for g = 1:length(name.GroupID)
  112. for s = 1:length(name.SubjID{g})
  113. for c = 1:length(name.CondID)
  114. if ~exist(paths.ICAcleanData{g}{s,c}, 'file')
  115. fprintf(ErrLog, ['ERROR: Missing ICA cleaned dataset for:'...
  116. '\n %s \n\n'], paths.ICAcleanData{g}{s,c});
  117. end
  118. end
  119. end
  120. end
  121. %=================%
  122. if exist([pwd,'/ErrorLog_ICA.txt'], 'file')
  123. LogCheck = dir('ErrorLog_ICA.txt');
  124. if LogCheck.bytes ~= 0 % File not empty
  125. open('ErrorLog_ICA.txt');
  126. else
  127. delete('ErrorLog_ICA.txt');
  128. end
  129. end
  130. fclose(ErrLog);
  131. diary off
  132. end
  133. function ParforSaveData(OutputPath, ICAcleanMEG)
  134. ICAcleanMEG
  135. save(OutputPath, 'ICAcleanMEG');
  136. end